﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.loto.Common.DBUtility;
using System.Data.SqlClient;
using com.huigo.Framework.Model;

namespace com.huigo.Framework.BLL
{
    public class PolicyInfoTravel
    {
        public static List<Model.PolicyInfoTravel> GetPolicyInfoTravelList(long orderFid)
        {
            List<string> fields = new List<string>() 
             {
                "POLICY_ID",
                "POLICY_ORDER_FID",
                "POLICY_HOLDER_NAME",
                "POLICY_HOLDER_CREDENTIAL_TYPE",
                "POLICY_HOLDER_CREDENTIAL_NO",
                "POLICY_HOLDER_BIRTH",
                "POLICY_HOLDER_GENDER"
                ,"POLICY_INSURED_NAME"
                ,"POLICY_INSURED_CREDENTIAL_TYPE"
                ,"POLICY_INSURED_CREDENTIAL_NO"
                ,"POLICY_INSURED_BIRTH"
                ,"POLICY_INSURED_GENDER"
                ,"POLICY_TRAVEL_LINE"
                ,"POLICY_FROM_PERIOD"
                ,"POLICY_INSURED_TEL"
                ,"POLICY_INSURED_EMAIL",
                "POLICY_STATUS"
                ,"POLICY_NO",
                "HG_POLICY_STATUS.STATUS_DESC",
                 "HG_POLICY_FEE_STATUS.STATUS_DESC AS FEE_STATUS_DESC,POLICY_FEE_STATUS,POLICY_UNIT_PRICE,POLICY_COUNT,POLICY_CUST_FID"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_POLICY_STATUS]","POLICY_STATUS=STATUS_ID"),
                   new JoinStatement("LEFT","[HG_POLICY_FEE_STATUS]","POLICY_FEE_STATUS=[HG_POLICY_FEE_STATUS].STATUS_ID")
         };
            List<WhereStatement> whereList = null;
            whereList = new List<WhereStatement>()
                {
                    new WhereStatement("POLICY_ORDER_FID","=",orderFid.ToString(),"")
               };
            List<Model.PolicyInfoTravel> PolicyInfoTravelList = null;
            PolicyInfoTravelList = SqlServerDal.PolicyInfoTravel.GetItems(fields, joinList, whereList, null, null);
            return PolicyInfoTravelList;

        }

        public static bool UpdatePolicyInfoTravel(long OrderID, int PolicyStatus)
        {
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                    new WhereStatement("POLICY_ORDER_FID","=",OrderID.ToString(),"")
            };

            Model.PolicyInfoTravel PolicyInfo = new Model.PolicyInfoTravel();
            PolicyInfo.PolicyStatus = PolicyStatus;
            return SqlServerDal.PolicyInfoTravel.Update(PolicyInfo, whereList, null);
        }
        public static Model.PolicyInfoTravel GetPolicyInfoTravelById(long id)
        {
            Model.PolicyInfoTravel policy = new Model.PolicyInfoTravel();
            policy.PolicyId = id;
            List<string> fields = new List<string>() 
             {
                "POLICY_ID",
                "POLICY_ORDER_FID",
                "POLICY_HOLDER_NAME",
                "POLICY_HOLDER_CREDENTIAL_TYPE",
                "POLICY_HOLDER_CREDENTIAL_NO",
                "POLICY_HOLDER_BIRTH",
                "POLICY_HOLDER_GENDER"
                ,"POLICY_INSURED_NAME"
                ,"POLICY_INSURED_CREDENTIAL_TYPE"
                ,"POLICY_INSURED_CREDENTIAL_NO"
                ,"POLICY_INSURED_BIRTH"
                ,"POLICY_INSURED_GENDER"
                ,"POLICY_TRAVEL_LINE"
                ,"POLICY_FROM_PERIOD"
                ,"POLICY_INSURED_TEL"
                ,"POLICY_INSURED_EMAIL",
                "POLICY_STATUS"
                ,"POLICY_NO",
                "STATUS_DESC"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_POLICY_STATUS]","POLICY_STATUS=STATUS_ID")
            };
            return SqlServerDal.PolicyInfoTravel.GetItem(policy, fields, joinList, null);
        }

        public static bool PolicyInfoTravelIsExisted(long cid, int policyStaus)
        {
            bool isExist = false;
            List<Model.PolicyInfoTravel> PolicyInfoTravelInfo = new List<Model.PolicyInfoTravel>();
            List<string> fields = new List<string>() 
            {
                "POLICY_ORDER_FID",
            };
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                new WhereStatement("POLICY_ID","=",cid.ToString(),"AND "),
                new WhereStatement("POLICY_STATUS","=","'"+policyStaus+"'","")
            };
            PolicyInfoTravelInfo = SqlServerDal.PolicyInfoTravel.GetItems(fields, whereList, null, null);
            if (PolicyInfoTravelInfo != null && PolicyInfoTravelInfo.Count > 0)
            {
                isExist = true;
            }
            return isExist;
        }
        public static bool UpdatePolicyInfoTravelStatus(int insType, string PolicyID, int policyStatusFid, long userID, string userName)
        {
            string[] arr = PolicyID.Split(',');
            foreach (string item in arr)
            {
                List<WhereStatement> whereList = new List<WhereStatement>()
                {
                    new WhereStatement("POLICY_ID","=",item,"")
                };
                Model.PolicyInfoTravel policyInfo = new Model.PolicyInfoTravel();
                policyInfo.PolicyStatus = policyStatusFid;
                int StatusBefore = 0;
                StatusBefore = GetPolicyInfoTravelById(Convert.ToInt64(item)).PolicyStatus;
                if (SqlServerDal.PolicyInfoTravel.Update(policyInfo, whereList, null))
                {
                    BLL.PolicyFlow.AddPolicyFlow(insType, Convert.ToInt64(item), userID, policyStatusFid, com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictPolicyStatus, policyStatusFid), null);
                    return true;
                }
            }
            return false;

        }
        /// <summary>
        /// 添加保险订单
        /// </summary>
        /// <param name="orderId">订单ID</param>
        /// <param name="holderName">投保人姓名</param>
        /// <param name="holderType">投保人证件类型</param>
        /// <param name="holderNo">投保人证件号</param>
        /// <param name="holderBirth">投保人出生日期</param>
        /// <param name="holderGender">投保人性别</param>
        /// <param name="insuredName">被保人姓名</param>
        /// <param name="insuredType">被保人证件类型</param>
        /// <param name="insuredNo">被保人证件号</param>
        /// <param name="insuredBirth">被保人出生日期</param>
        /// <param name="insuredGender">被保人性别</param>
        /// <param name="fromPeriod">保险起期</param>
        /// <param name="insurdeTel">被抱人手机</param>
        /// <param name="insuredEmail">被保人邮箱</param>
        /// <param name="count">购买份数</param>
        /// <returns></returns>
        public static long InsertPolicyInfoTravel(long orderId, long custFid, string holderName, string holderType, string holderNo, string holderBirth, string holderGender, string insuredName, string insuredType, string insuredNo, string insuredBirth, string insuredGender, string fromPeriod, int count, decimal price)
        {
            Model.PolicyInfoTravel policyInfoTravel = new com.huigo.Framework.Model.PolicyInfoTravel();
            policyInfoTravel.PolicyOrderFid = orderId;
            policyInfoTravel.PolicyHolderName = holderName;
            policyInfoTravel.PolicyHolderCredentialType = holderType;
            policyInfoTravel.PolicyHolderCredentialNo = holderNo;
            policyInfoTravel.PolicyHolderBirth = Convert.ToDateTime(Convert.ToDateTime(holderBirth).ToString("yyyy-MM-dd"));
            policyInfoTravel.PolicyHolderGender = holderGender;
            policyInfoTravel.PolicyInsuredName = insuredName;
            policyInfoTravel.PolicyInsuredCredentialType = insuredType;
            policyInfoTravel.PolicyInsuredCredentialNo = insuredNo;
            policyInfoTravel.PolicyInsuredBirth = Convert.ToDateTime(Convert.ToDateTime(insuredBirth).ToString("yyyy-MM-dd"));
            policyInfoTravel.PolicyInsuredGender = insuredGender;
            policyInfoTravel.PolicyFromPeriod = Convert.ToDateTime(Convert.ToDateTime(fromPeriod).ToString("yyyy-MM-dd"));
            policyInfoTravel.PolicyStatus = 1;
            policyInfoTravel.PolicyCustFid = custFid;
            policyInfoTravel.PolicyCount = count;
            policyInfoTravel.PolicyUnitPrice = price;
            policyInfoTravel.PolicyInsuredTel = " ";
            return SqlServerDal.PolicyInfoTravel.AddPolicy(policyInfoTravel, null);
        }

        public static long InsertPolicyInfoTravel(Model.PolicyInfoTravel policyInfoTravel, SqlTransaction trans)
        {
            return SqlServerDal.PolicyInfoTravel.AddPolicy(policyInfoTravel, trans);
        }

        public static Model.PolicyInfoTravel PolicyTravelByID(long policyTravelId)
        {
            Model.PolicyInfoTravel travel = new Model.PolicyInfoTravel();
            travel.PolicyId = policyTravelId;
            List<string> fields = new List<string>() 
             {  
                "POLICY_NO",
                "POLICY_OUT_ORDER_NO",
                "POLICY_TRADE_ORDER_NO",
                "ORDER_NO",
                "ORDER_PRODUCT_CODE",
                "ORDER_INS_TYPE_FID,POLICY_CUST_FID,POLICY_UNIT_PRICE,POLICY_COUNT,POLICY_FEE_STATUS,POLICY_STATUS,POLICY_OUT_ORDER_NO"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("INNER","[HG_ORDER_INFO]","ORDER_ID=POLICY_ORDER_FID")
            };
            return SqlServerDal.PolicyInfoTravel.GetItem(travel, fields, joinList, null);
        }

        //旅游险
        public static List<Model.PolicyInfoTravel> GetInvoiceOrderPolicyTravelCustName(long custFID,int insType,long orderID, int pageSize, int pageIndex, DateTime orderStart, DateTime orderEnd)
        {
            List<string> fields = new List<string>() 
             {
                  @"POLICY_INSURED_NAME"
                ,"POLICY_INSURED_CREDENTIAL_TYPE"
                ,"POLICY_INSURED_CREDENTIAL_NO"
                ,"POLICY_INSURED_BIRTH"
                ,"POLICY_INSURED_GENDER"
                ,"POLICY_TRAVEL_LINE"
                ,"POLICY_FROM_PERIOD"
                ,"POLICY_INSURED_TEL"
                ,"POLICY_INSURED_EMAIL,POLICY_NO,ORDER_NO"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_ORDER_INFO]","ORDER_ID=POLICY_ORDER_FID")
            };
            List<WhereStatement> whereList = null;
            whereList = new List<WhereStatement>();
            if (orderStart != DateTime.MinValue && orderEnd != DateTime.MinValue)
            {
                whereList.Add(new WhereStatement("ORDER_CREATED_ON", "BETWEEN", "'" + orderStart + "' AND '" + orderEnd + "'", "AND"));
            }
            whereList.Add(new WhereStatement("ORDER_INS_TYPE_FID", "=", insType.ToString(), " and"));
            whereList.Add(new WhereStatement("ORDER_CUST_FID", "=", custFID.ToString(), " and"));
            whereList.Add(new WhereStatement("POLICY_CUST_FID", "=", custFID.ToString(), " and"));
            whereList.Add(new WhereStatement("POLICY_STATUS", "=", ((int)Dict.PolicyStatus.InsuredSuc).ToString(), "  "));
           // whereList.Add(new WhereStatement("ORDER_CREATED_ON", ">=", "dateadd(mm,-1,getdate())", "  "));
            List<OrderByStatement> order=new List<OrderByStatement>();
            order.Add(new OrderByStatement("ORDER_CREATED_ON",false));
            List<Model.PolicyInfoTravel> PolicyInfoTravel = null;
            int recordCount = 0;
            int pageCount = 0;
            PolicyInfoTravel = SqlServerDal.PolicyInfoTravel.GetItemsByProc(fields, joinList, whereList, order, "[PROC_PAGINATION_OUT]", pageSize, pageIndex, out recordCount, out pageCount, null);
            PolicyInfoTravel.ForEach(delegate(Model.PolicyInfoTravel info)
            {
                info.RecordCount = recordCount;
                info.PageCount = pageCount;
            });
            return PolicyInfoTravel;
        }
    }
}
